<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\lib\exception\BaseException;
use app\model\Admins;
use app\model\Course as CourseModel;
use app\model\CourseCategory;

class Course extends BaseController
{
    public function index($limit=10)
    {
        $name = request()->param('name');
        $status = request()->param('status');
        $time = request()->param('time');
//        $admins=Admins::where('status',1)->select();
        $data= CourseModel::with(['category','teacher'])
            ->where('type',1)
            ->when($name, function ($query) use ($name) {
            $query->where('name', 'like', "%$name%");
        })->when($status, function ($query) use ($status) {
            $query->where('status', $status);
        })->when($time, function ($query) use ($time) {
            $query->whereBetween('create_time', $time);
        })->order('id','desc')->paginate($limit);
//        foreach ($data as &$v){
//            $v['teacher']=Admins::where('id',$v['admins_id'])->field('id,ename')->find()->toArray();
//        }
        return $this->success($data);
    }

    public function create()
    {
        $params = request()->param();
        $params['number'] = getNumber('課程');
        CourseModel::create($params);
        return $this->success();
    }

    public function update($id)
    {
        CourseModel::where('id',$id)->update(request()->param());
        return $this->success();
    }

    public function delete($id)
    {
        CourseModel::where('id','in',$id)->update(['type'=>-1]);
        return $this->success();
    }

    public function conduct()
    {
        $data=(request()->param('data'));
        $model =new CourseModel();
        $model->saveAll($data);
        return $this->success();
    }

    public function excelCreate()
    {

        $data = json_decode(request()->param('data'),true);
//        $data=request()->param('data');

        foreach ($data as $k=>$v){

           $CourseCategory= CourseCategory::where('name',$v['course_category'])->find();

           if(empty($CourseCategory)) throw new BaseException(['msg'=>'第'.($k+1).'列沒有該課程分類']);
//           $week=['星期一'=>1,'星期二'=>2,'星期三'=>3,'星期四'=>4,'星期五'=>5,'星期六'=>6,'星期日'=>7];
//           $w= explode(',',$v['week']);
           $admin= Admins::where('name',$v['admins'])->find();
            if(empty($admin)) throw new BaseException(['msg'=>'第'.($k+1).'列沒有該老師']);
            \app\model\Course::create([
                'name'=>$v['name'],
                'schooltime'=>$v['schooltime'],
                'quittingtime'=>$v['quittingtime'],
                'number'=>getNumber('課程'),
                'num'=>$v['num'],
                'admins_id'=>$admin->id,
                'remark'=>$v['remark'],
                'digest'=>$v['digest'],
                'price'=>$v['price'],
                'week'=>$v['week'],
                'course_category_id'=>$CourseCategory->id
            ]);
        }
        return $this->success();
    }
}
